package cn.jsu.oj.dispatchServer.thread;

import cn.jsu.oj.dispatchServer.factory.CmdHandleFactory;
import cn.jsu.oj.dispatchServer.handle.CommandPack;
import cn.jsu.oj.dispatchServer.init.AbstractClientInit;
import cn.jsu.oj.dispatchServer.init.ClientInitVector;
import cn.jsu.oj.dispatchServer.pojo.AbstractPack;

/**
 * @author ahtonc
 */
public class ClientHandleThread implements Runnable {

    private ClientInitVector clientInitVector;

    public ClientHandleThread(ClientInitVector clientInitVector) {
        this.clientInitVector = clientInitVector;
    }

    @Override
    public void run() {
        try {
            AbstractPack pack = (AbstractPack) clientInitVector.getMessageQueue().take();

            CommandPack<AbstractPack> commandPack = new CommandPack<>(clientInitVector.getChannel(), pack);

            CommandPack<?> cmdPack = CmdHandleFactory.getHandle(pack.getTypeNo()).handle(commandPack);

            AbstractClientInit init = (AbstractClientInit) cmdPack.getData();

            clientInitVector.setMessageQueue(init.getMessageQueue());

            init.run();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
